package com.sikaryofficial.backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sikaryofficial.backend.domain.dto.req.UserProtocolReq;
import com.sikaryofficial.backend.domain.dto.resp.ProtocolDTO;
import com.sikaryofficial.backend.domain.entity.UserProtocol;
import com.sikaryofficial.backend.domain.mapping.ProtocolMapping;
import com.sikaryofficial.backend.service.IUserProtocolService;
import com.sikaryofficial.common.core.utils.HiResultUtil;
import com.sikaryofficial.common.core.web.controller.BaseController;
import com.sikaryofficial.common.core.web.domain.HiResult;
import com.sikaryofficial.common.log.annotation.Log;
import com.sikaryofficial.common.log.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 协议Controller
 *
 * @author qinjinyuan
 * @date 2023-11-16
 */
@RestController
@RequestMapping("/user/protocol")
@Api(tags = "用户协议服务")
public class UserProtocolController extends BaseController {

    @Autowired
    private IUserProtocolService protocolService;

    /**
     * 获取协议详细信息
     */
    @PostMapping(value = "/view")
    @ApiOperation("获取协议,protocolType 1-用户协议,2-隐私协议")
    public HiResult<ProtocolDTO> getInfoByType(@RequestBody UserProtocolReq req)
    {
        return HiResultUtil.success(protocolService.getInfoByType(req.getProtocolType()));
    }

    /**
     * 修改协议
     */
    @Log(title = "协议", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    @ApiOperation("修改协议,protocolType 1-用户协议,2-隐私协议")
    public HiResult<Boolean> edit(@RequestBody ProtocolDTO protocol) {
        return HiResultUtil.success(protocolService.updateProtocol(protocol));
    }

    /**
     * 协议列表
     */
    @PostMapping("/list")
    @ApiOperation("协议列表")
    public HiResult<List<ProtocolDTO>> list() {
        List<UserProtocol> list = protocolService.list(new LambdaQueryWrapper<UserProtocol>()
                        .select(UserProtocol::getProtocolId,UserProtocol::getProtocolType,UserProtocol::getContent)
                .eq(UserProtocol::getDeletedVersion, 0));
        return HiResultUtil.success(ProtocolMapping.INSTANCE.coverListToResp(list));
    }
}
